<template>
  <div class="content-block block-half-1">
    <div class="info-head">{{ title }}</div>
    <div class="info-content" ref="container">
      <v-chart
        ref="chart"
        :option="chartOption"
        style="width: 100%; height: 400px"
      ></v-chart>
    </div>
  </div>
</template>

<script>
import ChartResizeListener from '@/mixins/chart-resize-listener';
import MixinChartOptionToolbox from '@/mixins/chart-option-toolbox';
import { defaultNumber } from '@/utils';

export default {
  name: 'grid-item-two',
  mixins: [ChartResizeListener, MixinChartOptionToolbox],
  props: {
    title: String,
    data: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data () {
    return {
      dataViewFirstCellName: '时间',
    }
  },
  computed: {
    lines () {
      return (this.data.series || []);
    },
    serie () {
      return this.lines[0] || {}
    },
    chartOption () {
      return {
        legend: {
          left: 'center',
          data: this.lines.map(v => v.name)
        },
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          // containLabel: true,
          top: '70px',
          // left: '80px',
          // right: '80px',
          // bottom: '30px',
        },
        toolbox: this.chartOptionToolbox,
        xAxis: {
          type: 'category',
          data: (this.data.xaxis || {}).data || []
        },
        yAxis: {
          type: 'value',
          name: this.serie.unit,
          axisLine: {
            show: true
          }
        },
        series: [
          ...this.lines.map((v, i) => ({
            name: v.name,
            type: 'line',
            smooth: true,
            tooltip: {
              valueFormatter: value => `${defaultNumber(value)} ${v.unit}`
            },
            data: v.data || []
          }))
        ]
      };
    }
  },
};
</script>
